from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.skus import SKUSerializer, CategorieSerializer, GoodsSimpleSerializer, SPUSpecificationSerializer
from meiduo_admin.utils import PageNum
from goods.models import SKU, GoodsCategory, SPU, SPUSpecification


class SKUViewSet(ModelViewSet):
    pagination_class = PageNum
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer

    def categorie(self,request):
        data=GoodsCategory.objects.filter(subs=None)
        ser=CategorieSerializer(data,many=True)
        return Response(ser.data)

    def goodssimple(self,request):
        data = SPU.objects.all()
        ser=GoodsSimpleSerializer(data,many=True)
        return Response(ser.data)

    def spuspecification(self,request,pk):
        # 查询spu商品的所有规格
        data=SPUSpecification.objects.filter(spu_id=pk)
        # 进行序列化返回
        ser = SPUSpecificationSerializer(data,many=True)
        return Response(ser.data)



